#include "InstrumentBackground.h"
#include "MLProgressbar.h"

constexpr double PI = 3.14159265358979323846;

_InstrumentFan::_InstrumentFan(
	VelocityStream& velocity_stream,
	int frame_size_x, int frame_size_y,
	size_t instruments_total_num,
	size_t instrument_index,
	Byte color_r,
	Byte color_g,
	Byte color_b,
	Byte alpha,
	GLuint glsl,
	GLuint mix_glsl,
	double brightness,
	double light_extension,
	int record_radius
) {
	_frame_size_x = frame_size_x; _frame_size_y = frame_size_y;
	_radius = sqrt(frame_size_x * frame_size_x + frame_size_y * frame_size_y) / 2;
	_stream = velocity_stream;
	_start_angle = 2 * PI * instrument_index / instruments_total_num;
	_end_angle = 2 * PI * (instrument_index + 1) / instruments_total_num;
	_color_r = color_r;
	_color_g = color_g;
	_color_b = color_b;
	_center_x = frame_size_x / 2;
	_center_y = frame_size_y / 2;
	_max_brightness_value = 0;
	_sector = Drawer::initSector(frame_size_x, frame_size_y, _start_angle, _end_angle, light_extension, std::clamp<double>(color_r * brightness, 0, 255), std::clamp<double>(color_g * brightness, 0, 255), std::clamp<double>(color_b * brightness, 0, 255), alpha);
	_glsl = glsl;
	_mix_glsl = mix_glsl;
	_start_radius = record_radius;
}

void _InstrumentFan::get_frame(size_t frame_index, MLFrame& frame) {
	Utils::clearFrame(_sector.msaaFbo);
	double brightness_val = _stream.get(frame_index) / 127;
	// _sector.resolveFbo = frame.fbo;
	// _sector.resolveTex = frame.texture;
	Drawer::drawSector(_glsl, _sector, brightness_val * _radius + _start_radius);
	frame.mix(_sector.resolveFbo, _sector.resolveTex, 0, _mix_glsl);
}

InstrumentBackground::InstrumentBackground(
	std::vector<VelocityStream>& velocity_streams,
	int frame_size_x, int frame_size_y,
	double frame_rate,
	std::map<std::string, Color>& colors,
	Byte alpha,
	double light_extension,
	double min_light_extension_angle,
	double brightness,
	int record_radius
) :MLObj(frame_size_x, frame_size_y, frame_rate) {
	std::cout << (char*)u8"正在初始化背景光效……\n" << std::endl;
	size_t instruments_total_num = velocity_streams.size();
	MLProgressbar progressbar(instruments_total_num, "初始化进度");
	GLuint glsl = Utils::activateShader("./GLSL/SectorVertex.glsl", "./GLSL/SectorFragment.glsl");
	_mix_glsl = Utils::activateShader("./GLSL/MixVertex.glsl", "./GLSL/MixFragment.glsl");
	double light_extension_angle = std::max(light_extension * PI * 2 / instruments_total_num, min_light_extension_angle * PI / 180);
	for (size_t i = 0; i < instruments_total_num; i++) {
		Color color = colors[velocity_streams[i].name];
		_instrument_fans.push_back(
			_InstrumentFan(
				velocity_streams[i],
				frame_size_x, frame_size_y,
				instruments_total_num,
				i,
				color.x,
				color.y,
				color.z,
				alpha,
				glsl,
				_mix_glsl,
				brightness,
				light_extension_angle,
				record_radius
			)
		);
		progressbar.update();
	}
}

void InstrumentBackground::get_frame(size_t frame_index, MLFrame& frame) {
	MLFrame new_frame(_frame_size_x, _frame_size_y);
	// glEnable(GL_BLEND);
	// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	for (size_t i = 0; i < _instrument_fans.size(); i++) {
		_instrument_fans[i].get_frame(frame_index, new_frame);
	}
	// Drawer::applyGaussianBlur(_blur_kernel, new_frame.fbo, new_frame.rbo, new_frame.texture);
	// glDisable(GL_BLEND);
	frame.mix(new_frame, _mix_glsl);
	new_frame.clear();
}
